package com.dao;

import com.bean.Friends;
import com.bean.User;
import com.db.DBUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FriendsDao {
    DBUtil dbUtil = new DBUtil();
    /**
     * 添加好友
     * @param friends
     */
    public void saveFriend(Friends friends){

        String sql="insert into friend(user_id,friend_id,comments) values" +
                "("+friends.getUserId()+","+friends.getFriendId()+","
                +"'"+friends.getComments()+"')";
        try {
            dbUtil.insertOrUpdate(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbUtil.close();
        }
    }

    /**
     * 更具userid查找好友
     * @param userId
     * @return
     */
    public List<Friends> findFriends(int userId){
        String sql="select * from friend where user_id="+userId;
        List<Friends> list = new ArrayList<Friends>();
        try {
            ResultSet result=dbUtil.query(sql);
            while (result.next()){
                Friends friends = new Friends();
                friends.setId(result.getInt("id"));
                friends.setUserId(result.getInt("user_id"));
                friends.setFriendId(result.getInt("friend_id"));
                friends.setUser(findUser(result.getInt("friend_id")));
                list.add(friends);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbUtil.close();
        }
        return list;
    }

    /**
     * 查找 好友friends 是否已经存在
     * @param userId
     * @param friendId
     * @return
     */
    public boolean findFriendsExits(int userId,int friendId){
        String sql="select count(1) as num from friend  where user_id="+userId+ " and friend_id="+friendId;
        int num =0;
        try {
            ResultSet result=dbUtil.query(sql);
            while (result.next()){
                num= result.getInt("num");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbUtil.close();
        }
        return num>0?true:false;
    }

    /**
     * 查找用户
     * @param id
     * @return
     */
    public User findUser(int id){
        String sql="select * from  users where id="+id;
        User c = new User();
        try {
            ResultSet result=dbUtil.query(sql);
            while (result.next()){
                c.setId(result.getInt("id"));
                c.setUsername(result.getString("username"));
                c.setPassword(result.getString("password"));
                c.setAge(result.getString("age"));
                c.setHeadPic(result.getString("headpic"));
                c.setSex(result.getString("sex"));
                c.setQq(result.getString("qq"));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return c;
    }

    /**
     * 删除好友
     * @param userId
     * @param friendId
     */
    public void delete(int userId, int friendId) {
        String sql="delete from friend  where user_id="+userId+ " and friend_id="+friendId;
        try {
            dbUtil.delete(sql);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            dbUtil.close();
        }
    }
}
